package com.acme.bankapp.domain;
import java.io.Serializable;
import java.util.*;

import com.acme.bankapp.exceptions.*;
import com.acme.bankapp.service.*;


public class Bank implements Serializable{
	private Set<Client> clients;
	transient private ClientRegistrationListener[] listeners = new ClientRegistrationListener[10];
	private int numberOfListeners;
	
	public int getNumberOfClients() {
		return clients.size();
	}
	

	public Set<Client> getClients() {
		return Collections.unmodifiableSet(clients);
	}

	public Bank(ClientRegistrationListener[] listeners, int maxAmoutOfClients) {
		clients = new HashSet<Client>();
		this.listeners = listeners.clone();
		numberOfListeners = listeners.length - 1;
		this.listeners[numberOfListeners++] = new ClientRegistrationListener() {
			public void onClientAdded(Client client) {
				client.getClientSituation();
				System.out.println(new java.util.Date());
			}
		};
	}
	
	public void addClient(Client client) throws ClientExistsException {
		for (Client cli : clients) {
			if (cli.equals(client))
				throw new ClientExistsException("Client "+client.getClientSituation()+" exists");
		}
		clients.add(client);
		for (int i = 0; i < numberOfListeners; i++) {
			listeners[i].onClientAdded(client);
		}
	}
	
}
